Public Class Form1
    Public Small_w As Integer = My.Settings.MinLen   'smallest word
    Public Large_w As Integer = My.Settings.MaxLen   'largest word
    Public wrong_g As Integer = 0 '# of wrong guesses
    Public Correct_g As Integer = 0
    Public Curword As String = "" 'current word
    Public Cntrs As List(Of Control) 'list of labels
    Public Correct As Integer = 0 'correct letters
    Public Foc As Double = 0.01
    Public UP As Boolean = True
    'Public wrong_g As Integer = 0 '# of wrong guesses used to determine hangman pict
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NewGame()
        Ind = 0
        CreateLetters()
        Record.Text = My.Settings.Record1

    End Sub
    Public Sub NewGame()
        Dim a As New Dictionary.CDict
        a.DictNum = My.Settings.Dictionary
        Randomize()
        Dim rand As New Random(Rnd() * 100)
        '150:    Randomize(Rnd() * 100)
        '        Dim rand As New Random(Rnd() * 1000)
        '        Curword = a.RandWord(rand.Next(0, 50000))
        Dim strin As String = ""

        For i = 1 To rand.Next(Small_w, Large_w + 1)
            strin = strin & "?"
        Next
        Dim w As New List(Of String)
150:    'Curword = a.RandWord (1)
        a.FindMatches(strin)
        For Each word In a.Words
            w.Add(word)
        Next
        Curword = w.Item(rand.Next(0, w.Count))
        'If Len(Curword) < Small_w Or Len(Curword) > Large_w Then
        '    GoTo 150
        'End If

        tb1.Text = Curword
        'width of each letter = 25
        Dim bmp As New Bitmap((29 * Len(Curword)), 20)
        pb1.Image = bmp
        pb1.Size = bmp.Size
        pb1.Location = New Point((Me.Width / 2) - (bmp.Width / 2), pb1.Location.Y)
        Dim graph As Graphics = Graphics.FromImage(bmp)
        x1 = -27
        y1 = 20
        Dim pen As New Pen(Color.Black)
        pen.Width = 2
        For i = 1 To Len(Curword)
            x1 = x1 + 29
            graph.DrawLine(pen, x1, y1, x1 + 25, y1)
        Next
        'bmp = ReflectionLib.ApplyReflection(bmp, 25, 0, 255, 25)
        pb1.Image = bmp
        CreateLetters()
        Stats.Visible = False
        lstLetters.Enabled = True
        pb2.Image = New Bitmap(pb2.Width, pb2.Height)
        Wrong_g = 0
        Correct_g = 0
        Correct = 0
        Timer1.Enabled = True
    End Sub
    Public Sub EndGame()
        Timer1.Enabled = False
        Dim bmp As New Bitmap(Stats.Width, Stats.Height)
        Dim graph As Graphics = Graphics.FromImage(bmp)
        'Dim p As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(bmp.Width, bmp.Height), Color.Wheat, Color.Black)
        Dim clr As New Color
        Dim clr1 As New Color
        If wrong_g >= 7 Then
            MsgBox("Sorry, But you lost this game of HangMan. After " & wrong_g & " tries, you couldn't guess that the correct word was:" & (Chr(13)) & UCase(Curword), MsgBoxStyle.Information, "You Lose!")
            My.Settings.GameCount = My.Settings.GameCount + 1
            clr = Color.Red
            clr1 = Color.Firebrick
        Else : clr = Color.Green
            clr1 = Color.LightGreen
            MsgBox("Congradulations!, You won this game of hangman, after only " & wrong_g & " wrong guesses, and found the correct word to be:" & (Chr(13)) & UCase(Curword), MsgBoxStyle.Exclamation, "You win!!")
            'Records.Show()
            My.Settings.GameCount = My.Settings.GameCount + 1
        End If
        Dim p As New Drawing2D.LinearGradientBrush(New Rectangle(0, 0, bmp.Width, bmp.Height), Color.Transparent, clr, Drawing2D.LinearGradientMode.Horizontal)

        p.SetSigmaBellShape(0.85)
        graph.FillRectangle(p, 0, 0, bmp.Width, bmp.Height)
        Stats.BackgroundImage = bmp
        totalGuesses.Text = Correct_g + wrong_g
        CorrectGuesses.Text = Correct_g
        PercentCorrect.Text = FormatPercent(Correct_g / Val(totalGuesses.Text), 2)
        WrongGuesses.Text = wrong_g
        percentWrong.Text = FormatPercent(wrong_g / Val(totalGuesses.Text), 2)
        CorrectWord.Text = UCase(Curword)

        Stats.Visible = True
        lstLetters.Enabled = False
        Button1.Enabled = False
    End Sub
    Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
        NewGame()
        lstLetters.Enabled = True
        Ind = 0
    End Sub
    Public Sub AddLetter(ByVal NonZeroindex As Integer)
        Dim a As String = Mid(Curword, NonZeroindex, 1)
        Dim bmp As Bitmap = pb1.Image
        Dim graph As Graphics = Graphics.FromImage(bmp)
        Dim pt As Point = New Point(29 * NonZeroindex - 29, 0)
        Dim b As New Drawing2D.LinearGradientBrush(New Rectangle(0, 0, bmp.Width, bmp.Height), Color.Black, Color.Black, Drawing2D.LinearGradientMode.Horizontal)
        graph.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        graph.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
        graph.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
        graph.DrawString(a, tb1.Font, b, pt)
        'bmp = ReflectionLib.ApplyReflection(bmp, 25, 0, 255, 25)
        pb1.Image = bmp
        Correct = Correct + 1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Ind = Ind + 1
        'AddLetter(Ind)
        'CreateLetters()

        index = lstLetters.SelectedIndex
        a = lstLetters.Items.Item(index)
        a = LCase(a)
        If a = "**" Then
            Exit Sub
        Else
            lstLetters.Items.RemoveAt(index)
            lstLetters.Items.Insert(index, "**")
            Button1.Enabled = False
        End If
        If Curword.Contains(a) = False Then
            DrawHangMan()
        Else 'is in word
            Correct_g = Correct_g + 1
            If Curword.Contains(a) = True Then
                b = InStr(Curword, a)
50:             AddLetter(b)
                b = InStr(b + 1, Curword, a)
                If b = 0 Then

                Else : GoTo 50
                End If

            End If
            If Correct = Len(Curword) Then
                'win game
                'totalGuesses.Text = Correct_g + wrong_g
                'CorrectGuesses.Text = Correct_g
                'PercentCorrect.Text = FormatPercent(Correct_g / Val(totalGuesses.Text), 2)
                'WrongGuesses.Text = wrong_g
                'percentWrong.Text = FormatPercent(wrong_g / Val(totalGuesses.Text), 2)
                'CorrectWord.Text = UCase(Curword)
                EndGame()
                Stats.Show()
            End If
        End If


    End Sub

    Public Sub CreateLetters()
        lstLetters.Items.Clear()
        lstLetters.Items.Add("A")
        lstLetters.Items.Add("B")
        lstLetters.Items.Add("C")
        lstLetters.Items.Add("D")
        lstLetters.Items.Add("E")
        lstLetters.Items.Add("F")
        lstLetters.Items.Add("G")
        lstLetters.Items.Add("H")
        lstLetters.Items.Add("I")
        lstLetters.Items.Add("J")
        lstLetters.Items.Add("K")
        lstLetters.Items.Add("L")
        lstLetters.Items.Add("M")
        lstLetters.Items.Add("N")
        lstLetters.Items.Add("O")
        lstLetters.Items.Add("P")
        lstLetters.Items.Add("Q")
        lstLetters.Items.Add("R")
        lstLetters.Items.Add("S")
        lstLetters.Items.Add("T")
        lstLetters.Items.Add("U")
        lstLetters.Items.Add("V")
        lstLetters.Items.Add("W")
        lstLetters.Items.Add("X")
        lstLetters.Items.Add("Y")
        lstLetters.Items.Add("Z")
    End Sub

    Private Sub lstLetters_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstLetters.SelectedIndexChanged
        On Error GoTo 150
        If lstLetters.Items.Item(lstLetters.SelectedIndex) = "**" Then
            Button1.Enabled = False
        Else : Button1.Enabled = True
        End If
        Exit Sub
150:
    End Sub
    Public Sub DrawHangMan()
        Dim x1 As Integer
        Dim y1 As Integer
        Dim x2 As Integer
        Dim y2 As Integer
        Dim bmp As New Bitmap(pb2.Width, pb2.Height)
        Dim graph As Graphics = Graphics.FromImage(bmp)
        wrong_g = wrong_g + 1
        a = wrong_g
        Dim clr As Color = Color.Red
        Dim pen As New Pen(clr, 2)


        Select Case a
            Case 1
                clr = Color.Red
                clr = Color.FromArgb(100, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)
            Case 2
                clr = Color.Red
                clr = Color.FromArgb(115, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)

                x1 = bmp.Width / 4
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = 15
                graph.DrawLine(pen, x1, y1, x2, y2)
            Case 3
                clr = Color.Red
                clr = Color.FromArgb(130, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)

                x1 = bmp.Width / 4
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = bmp.Width * (3 / 4)
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = bmp.Height * (2 / 8) + 15
                graph.DrawLine(pen, x1, y1, x2, y2)
            Case 4
                clr = Color.Red
                clr = Color.FromArgb(170, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)

                x1 = bmp.Width / 4
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = bmp.Width * (3 / 4)
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = bmp.Height * (2 / 8) + 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x2 - 25
                y1 = y2
                Dim rect As New Rectangle(x1, y1, 50, 50)
                graph.DrawArc(pen, rect, 0, 360)
            Case 5
                clr = Color.Red
                clr = Color.FromArgb(190, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)

                x1 = bmp.Width / 4
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = bmp.Width * (3 / 4)
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = bmp.Height * (2 / 8) + 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x2 - 25
                y1 = y2
                Dim rect As New Rectangle(x1, y1, 50, 50)
                graph.DrawArc(pen, rect, 0, 360)

                x1 = x2
                y1 = y2 + 50
                x2 = x1
                y2 = y1 + 60
                graph.DrawLine(pen, x1, y1, x2, y2)
            Case 6
                clr = Color.Red
                clr = Color.FromArgb(230, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)

                x1 = bmp.Width / 4
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = bmp.Width * (3 / 4)
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = bmp.Height * (2 / 8) + 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x2 - 25
                y1 = y2
                Dim rect As New Rectangle(x1, y1, 50, 50)
                graph.DrawArc(pen, rect, 0, 360)

                x1 = x2
                y1 = y2 + 50
                x2 = x1
                y2 = y1 + 60
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x2
                y1 = y2
                x2 = x1 - 15
                y2 = y1 + 15
                graph.DrawLine(pen, x1, y1, x2, y2)
                x2 = x1 + 15
                y2 = y1 + 15
                graph.DrawLine(pen, x1, y1, x2, y2)
            Case 7 '****GAME OVER****
                clr = Color.Red
                clr = Color.FromArgb(255, clr)
                pen.Color = clr
                x1 = bmp.Width / 4
                y1 = bmp.Height
                x2 = bmp.Width / 4
                graph.DrawLine(pen, x1, y1, x2, 15)

                x1 = bmp.Width / 4
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = bmp.Width * (3 / 4)
                y1 = 15
                x2 = bmp.Width * (3 / 4)
                y2 = bmp.Height * (2 / 8) + 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x2 - 25
                y1 = y2
                Dim rect As New Rectangle(x1, y1, 50, 50)
                graph.DrawArc(pen, rect, 0, 360)

                x1 = x2
                y1 = y2 + 50
                x2 = x1
                y2 = y1 + 60
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x2
                y1 = y2
                x2 = x1 - 15
                y2 = y1 + 15
                graph.DrawLine(pen, x1, y1, x2, y2)
                x2 = x1 + 15
                y2 = y1 + 15
                graph.DrawLine(pen, x1, y1, x2, y2)

                x1 = x1
                y1 = bmp.Height * (3 / 4) - 15
                x2 = x1 + 15
                y2 = y1 - 15
                graph.DrawLine(pen, x1, y1, x2, y2)
                x1 = x1
                y1 = bmp.Height * (3 / 4) - 15
                x2 = x1 - 15
                y2 = y1 - 15
                graph.DrawLine(pen, x1, y1, x2, y2)
                pb2.Image = bmp
                Button1.Enabled = False
                lstLetters.Enabled = False
                EndGame()
        End Select
        pb2.Image = bmp
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        NewGame()
    End Sub
    Private Sub Stats_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Stats.VisibleChanged
        If PercentCorrect.Text = "" Then
            PercentCorrect.Text = "0.00%"
        End If
        a = Mid(PercentCorrect.Text, 1, InStr(PercentCorrect.Text, "%") - 1)
        If Stats.Visible = True Then
        Else : Exit Sub
        End If
        If Val(a) >= My.Settings.Record1 Then
            My.Settings.Rec5 = My.Settings.Rec4
            My.Settings.Rec4 = My.Settings.Rec3
            My.Settings.Rec3 = My.Settings.Rec2
            My.Settings.Rec2 = My.Settings.Rec1
            My.Settings.Rec1 = InputBox("Congratulations!! You have the high score for HangMan! Please enter your name below:", "High Score", "Default")


            My.Settings.Record5 = My.Settings.Record4
            My.Settings.Record4 = My.Settings.Record3
            My.Settings.Record3 = My.Settings.Record2
            My.Settings.Record2 = My.Settings.Record1
            'Records.Show()
            My.Settings.Record1 = Val(a)
        ElseIf Val(a) >= My.Settings.Record2 And Val(a) < My.Settings.Record1 Then
            'second high score
            My.Settings.Rec5 = My.Settings.Rec4
            My.Settings.Rec4 = My.Settings.Rec3
            My.Settings.Rec3 = My.Settings.Rec2
            My.Settings.Rec2 = InputBox("Congratulations!! You have the Second high score for HangMan! Please enter your name below:", "High Score", "Default")
            My.Settings.Record5 = My.Settings.Record4
            My.Settings.Record4 = My.Settings.Record3
            My.Settings.Record3 = My.Settings.Record2
            My.Settings.Record2 = Val(a)
            'Records.Show()
        ElseIf Val(a) >= My.Settings.Record3 And Val(a) < My.Settings.Record2 Then
            My.Settings.Rec5 = My.Settings.Rec4
            My.Settings.Rec4 = My.Settings.Rec3
            My.Settings.Rec3 = InputBox("Congratulations!! You have the Third high score for HangMan! Please enter your name below:", "High Score", "Default")
            My.Settings.Record5 = My.Settings.Record4
            My.Settings.Record4 = My.Settings.Record3
            My.Settings.Record3 = Val(a)
            'Records.Show()
        ElseIf Val(a) >= My.Settings.Record4 And Val(a) < My.Settings.Record3 Then
            My.Settings.Rec5 = My.Settings.Rec4
            My.Settings.Rec4 = InputBox("Congratulations!! You have the Fourth high score for HangMan! Please enter your name below:", "High Score", "Default")

            My.Settings.Record5 = My.Settings.Record4
            My.Settings.Record4 = Val(a)
            'Records.Show()
        ElseIf Val(a) >= My.Settings.Record5 And Val(a) < My.Settings.Record4 Then
            My.Settings.Rec5 = InputBox("Congratulations!! You have the Fifth high score for HangMan! Please enter your name below:", "High Score", "Default")
            My.Settings.Record5 = Val(a)
            'Records.Show()
        End If

        Record.Text = My.Settings.Record1 & "%"
        Records.LoadRecords()
        If Val(a) >= My.Settings.Record5 Then
            Records.Show()
        End If
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Records.Show()
        'RecordsFRM.Show()
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        Options.Show()

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Records.Show()
    End Sub
    Public Sub ShowRecords()
        Records.Close()
        Records.Show()
    End Sub

    Private Sub pb1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pb1.Click
        txtCodes.Focus()
        txtCodes.Text = ""
    End Sub

    Private Sub txtCodes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCodes.TextChanged
        If txtCodes.Text = "show the current word" Then
            tb1.Visible = True
            txtCodes.Text = ""
        ElseIf txtCodes.Text = "show all the words" Then

        End If
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        FRMAbout.Show()
    End Sub

    Private Sub HelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem.Click

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim b1 As New LinearGradientBrush(New Rectangle(0, 0, pb2.Width, pb2.Height), Color.Black, Color.White, LinearGradientMode.Horizontal)
        Dim c1 As New ColorBlend
        c1.Colors = New Color() {Color.Transparent, Color.FromArgb(150, Color.Red), Color.Transparent}
        c1.Positions = New Single() {0.0, Foc, 1.0}
        b1.InterpolationColors = c1
        Dim bmp As New Bitmap(pb2.Width, pb2.Height)
        Dim graph As Graphics = Graphics.FromImage(bmp)
        graph.FillRectangle(b1, New Rectangle(0, 0, pb2.Width, pb2.Height))
        graph.SmoothingMode = SmoothingMode.HighQuality
        If UP = True Then
            Foc = Foc + 0.01
        Else : Foc = Foc - 0.01
        End If
        If Foc > 0.9 Then
            Timer1.Interval = 30
        ElseIf Foc > 0.7 Then
            Timer1.Interval = 15
        ElseIf Foc < 0.1 Then
            Timer1.Interval = 15
        ElseIf Foc < 0.3 Then
            Timer1.Interval = 30
        Else : Timer1.Interval = 10

        End If
        If Foc >= 1.0 Then
            UP = False
            Foc = 0.9
        ElseIf Foc <= 0.0 Then
            UP = True
            Foc = 0.1
        End If
        pb2.BackgroundImage = bmp
    End Sub
End Class
